package cn.wzut.edut.controller;

import cn.wzut.edut.jwt.CurrentUser;
import cn.wzut.edut.jwt.CurrentUserInfo;
import cn.wzut.edut.pojo.JsonResult;
import cn.wzut.edut.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * 组织控制层
 * by 叶庭辉
 */
@Validated
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    /**
     * 新增学院请求
     * @param currentUserInfo
     * @param deptName
     * @return
     */
    @PostMapping("/create/college")
    public JsonResult createCollege(@CurrentUser @NotNull CurrentUserInfo currentUserInfo,
                                    @RequestParam(value = "dept_name") @Pattern(regexp = "[\\u4e00-\\u9fa5]{2,20}", message = "学院名错误") @NotNull(message = "学院名不能为空") String deptName){
        return new JsonResult(deptService.createCollege(currentUserInfo.getUserId(), deptName));
    }

    /**
     * 新增组织请求
     * @param currentUserInfo
     * @param deptName
     * @param parentId
     * @param deptType
     * @return
     */
    @PostMapping("/create/class")
    public JsonResult createDept(@CurrentUser @NotNull CurrentUserInfo currentUserInfo,
                                  @RequestParam(value = "dept_name") @Pattern(regexp = "[\\u4e00-\\u9fa5]{2,20}", message = "组织名错误") @NotNull(message = "组织名不能为空") String deptName,
                                  @RequestParam(value = "parent_dept_id") String parentId,
                                  @RequestParam(value = "dept_type") @Pattern(regexp = "^(学院|行政班级|虚拟班级|教师组)$", message = "组织类型错误") @NotNull(message = "组织类型不能为空") String deptType){
        return new JsonResult(deptService.createDept(currentUserInfo.getUserId(), deptName, parentId, deptType));
    }

    /**
     * 删除组织请求
     * @param deptId
     * @return
     */
    @PostMapping("/delete")
    public JsonResult delDept(@RequestParam(value = "id") @NotNull String[] deptId){
        return new JsonResult(deptService.delDept(deptId));
    }

    /**
     * 组织修改用户请求
     * @param deptId
     * @param addUserIds
     * @param delUserIds
     * @return
     */
    @PostMapping("/update/stu")
    public JsonResult updateDeptUser(@RequestParam(value = "id") @NotNull String deptId,
                                     @RequestParam(value = "add_stuarray") @NotNull String[] addUserIds,
                                     @RequestParam(value = "del_stuarray") @NotNull String[] delUserIds){
        return new JsonResult(deptService.updateDeptUser(deptId, addUserIds, delUserIds));
    }

    /**
     * 组织更新信息请求
     * @param deptId
     * @param deptName
     * @return
     */
    @PostMapping("/update/info")
    public JsonResult updateDeptInfo(@RequestParam(value = "id") @NotNull String deptId,
                                     @RequestParam(value = "dept_name") @Pattern(regexp = "[\\u4e00-\\u9fa5]{2,20}", message = "组织名错误") @NotNull(message = "组织名不能为空") String deptName){
        return new JsonResult(deptService.updateDeptInfo(deptId, deptName));
    }

    /**
     * 获取学院分页列表请求
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/get/college")
    public JsonResult getCollege(@RequestParam(value = "pagenum") @NotNull long current,
                                 @RequestParam(value = "pagesize") @NotNull long size){
        return new JsonResult(deptService.getCollege(current, size));
    }

    /**
     * 获取当前用户拥有的组织分页列表请求
     * @param currentUserInfo
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/get/class")
    public JsonResult getDeptByUser(@CurrentUser @NotNull CurrentUserInfo currentUserInfo,
                                     @RequestParam(value = "pagenum") @NotNull long current,
                                     @RequestParam(value = "pagesize") @NotNull long size){
        return new JsonResult(deptService.getDeptByUser(currentUserInfo.getUserId(), current, size));
    }

    /**
     * 根据组织名获取组织列表
     * @param deptName
     * @param deptType
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/get/list_by_name")
    public JsonResult getDeptByNameAndType(@RequestParam(value = "dept_name") @Pattern(regexp = "[\\u4e00-\\u9fa5]{2,20}", message = "组织名错误") @NotNull(message = "组织名不能为空") String deptName,
                                           @RequestParam(value = "dept_type") @Pattern(regexp = "^(学院|行政班级|虚拟班级|教师组)$", message = "组织类型错误") @NotNull(message = "组织类型不能为空") String deptType,
                                           @RequestParam(value = "pagenum") @NotNull long current,
                                           @RequestParam(value = "pagesize") @NotNull long size){
        return new JsonResult(deptService.getDeptByNameAndType(deptName, deptType, current, size));
    }

    /**
     * 获取所有的学院、行政班级和虚拟班级请求
     * @return
     */
    @GetMapping("/get/college/adminclass")
    public JsonResult getAllClassAndDept(){
        return new JsonResult(deptService.getAllClassAndDept());
    }

    /**
     * 获取所有学院和教师组请求
     * @return
     */
    @GetMapping("/get/college/tchgroup")
    public JsonResult getAllGroupAndDept(){
        return new JsonResult(deptService.getAllGroupAndDept());
    }

}
